package org.ceteca.explica.client.gui;

import java.util.Date;

import org.apache.log4j.Logger;
import org.ceteca.explica.client.gui.common.CommonClient;
import org.ceteca.explica.core.InterfaceExplica;
import org.ceteca.explica.core.util.Converter;
import org.ceteca.javaglade.InterfaceGladeGtk;
import org.ceteca.javaglade.JavaGladeException;
import org.ceteca.javaglade.handlers.GenericHandler;
import org.ceteca.javaglade.toolkit.swt.SwtDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.DateTime;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;

public class ExplicaDateSelect extends GenericHandler {

	private String dateSelectGUIName;
	private SwtDialog dialogDateSelect;
	private Logger logger;
	private Date date;
	private DateTime calendar;
	private CommonClient entityLauncher;
	private Button okButton, cancelButton;
	private String selectionMethod;
	
	
	//public ExplicaDateSelect() {
	//	super();
		
	//}
	public ExplicaDateSelect(Logger log, CommonClient el, String selMethod){
		super();
		this.logger=log;
		this.entityLauncher=el;
		this.dateSelectGUIName = entityLauncher.getEditionGui().getGuiName() + "\\"
		+ "Explica_DateSelect";
		this.selectionMethod=selMethod;
		
	}
	
	public void build(){
		

		try {
			this.gui = ExplicaGUIBuilder.getInstance().buildGUI(
					dateSelectGUIName,
					InterfaceExplica.EXPLICA_DATE_SELECT_GLADE_PATH,
					this.entityLauncher.getEditionGui(),
					this.entityLauncher.getEditWindow());

//			this.setJavaGlade(ExplicaGUIBuilder.getInstance()
//					.getGUI(dateSelectGUIName));
			getWidgets();
			Listener[] list=cancelButton.getListeners(SWT.Selection);
			for (Listener l:list){
				cancelButton.removeListener(SWT.Selection, l);
				
			}
			Listener[] list2=okButton.getListeners(SWT.Selection);
			for (Listener l:list2){
				okButton.removeListener(SWT.Selection, l);
				
			}
			//cancelButton.removeSelectionListener(null);
			ExplicaGUIBuilder.getInstance().connectSignal(InterfaceGladeGtk.GTK_SIGNAL_TYPE_CLICKED, cancelButton, "on_cancel_button_clicked", this);
			ExplicaGUIBuilder.getInstance().connectSignal(InterfaceGladeGtk.GTK_SIGNAL_TYPE_CLICKED, okButton, "on_ok_button_clicked", this);
			ExplicaGUIBuilder.getInstance().connectSignal(InterfaceGladeGtk.GTK_SIGNAL_TYPE_CLICKED, okButton, selectionMethod, entityLauncher);

		} catch (JavaGladeException e) {
			logger.error("Error building Map Dialog");
			e.printStackTrace();

		}
		
	}
	
	
	private void getWidgets(){
		if (this.gui != null){
			calendar=(DateTime)this.gui.getWidget(InterfaceExplica.SELECT_DATE_CALENDAR_WIDGETNAME);
			dialogDateSelect = this.gui.getDialog(
					InterfaceExplica.EXPLICA_DATE_SELECT_DIALOG_NAME);
			okButton=(Button)this.gui.getWidget(InterfaceExplica.SELECT_DATE_OK_BUTTON_WIDGETNAME);
			cancelButton=(Button)this.gui.getWidget(InterfaceExplica.SELECT_DATE_CANCEL_BUTTON_WIDGETNAME);
		}
		if (calendar== null || dialogDateSelect==null || okButton==null || cancelButton==null)
			System.exit(-1);
	}
	
	
	public void show(Date d){
		org.ceteca.explica.core.util.DateTime dt=new org.ceteca.explica.core.util.DateTime();
		dt.setDate(d);
		this.date=d;
		calendar.setDate(dt.getYearAsInt(), dt.getMonthAsInt(), dt.getDayAsInt());
		dialogDateSelect.open();
	}
	
	
	public void on_cancel_button_clicked(Event e){
		
		dialogDateSelect.close();
	}
	
	public void on_ok_button_clicked(Event e){
		setDate(Converter.getInstance().toDate(calendar));
		System.out.println("Seleccinada nueva fecha..."+getDate());
		dialogDateSelect.close();

	}

	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}
	
	
}
